<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="名称" prop="name">
				<el-input
					v-model="queryParams.name"
					placeholder="请输入名称"
					clearable
					size="small"
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>
			<el-form-item label="类型" prop="type">
				<el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small">
					<el-option label="医生" value="doctor"/>
					<el-option label="患者" value="patient"/>
				</el-select>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
					<el-option label="启用" :value="0"/>
					<el-option label="禁用" :value="1"/>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="primary"
					icon="el-icon-plus"
					size="mini"
					@click="handleAdd"
				>新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="success"
					icon="el-icon-edit"
					size="mini"
					:disabled="single"
					@click="handleUpdate"
				>修改
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="danger"
					icon="el-icon-delete"
					size="mini"
					:disabled="multiple"
					@click="handleDelete"
				>删除
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button
					type="warning"
					icon="el-icon-download"
					size="mini"
					@click="handleExport"
				>导出
				</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center"/>
			<el-table-column label="名称" align="center" prop="name"/>
			<el-table-column label="排序" align="center" prop="sort"/>
			<el-table-column label="跳转链接" align="center" prop="link"/>
			<el-table-column label="跳转appid" align="center" prop="appid"/>
			<el-table-column label="缩略图" align="center" prop="thumbnail">
				<template v-slot="scope">
					<el-image class="image" :src="ossUrl + scope.row.thumbnail" style="width: 100px;height: 50px;"></el-image>
				</template>
			</el-table-column>
			<el-table-column label="类型" align="center" prop="type">
				<template v-slot="scope">
					<span v-if="scope.row.type === 'doctor'">医生端</span>
					<span v-if="scope.row.type === 'patient'">患者端</span>
				</template>
			</el-table-column>
			<el-table-column label="状态" align="center" prop="status">
				<template v-slot="scope">
					<span v-if="scope.row.status === 0">启用</span>
					<span v-if="scope.row.status === 1">禁用</span>
				</template>
			</el-table-column>
			<el-table-column label="创建时间" align="center" prop="createTime"/>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button
						size="mini"
						type="text"
						icon="el-icon-edit"
						@click="handleUpdate(scope.row)"
					>修改
					</el-button>
					<el-button
						size="mini"
						type="text"
						icon="el-icon-delete"
						@click="handleDelete(scope.row)"
					>删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>

		<!-- 添加或修改banner对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入名称"/>
				</el-form-item>
				<el-form-item label="跳转链接" prop="link">
					<el-input v-model="form.link" placeholder="请输入小程序链接"/>
				</el-form-item>
				<el-form-item label="跳转appid" prop="appid">
					<el-input v-model="form.appid" placeholder="请输入跳转的小程序appid"/>
				</el-form-item>
				<el-form-item label="缩略图" prop="thumbnail">
					<el-upload
						class="video_uploader"
						:action="upload.url"
						:headers="upload.headers"
						:show-file-list="false"
						:on-success="handleImageSuccess"
						:before-upload="beforeImageUpload">
						<img v-if="form.thumbnail" :src="ossUrl + form.thumbnail" class="avatar" alt="">
						<i v-else class="el-icon-plus video_uploader-icon"></i>
					</el-upload>
				</el-form-item>
				<el-form-item label="类型" prop="type">
					<el-radio-group v-model="form.type">
						<el-radio label="doctor">医生端</el-radio>
						<el-radio label="patient">患者端</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="form.status">
						<el-radio :label="0">启用</el-radio>
						<el-radio :label="1">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="排序" prop="sort">
					<el-input v-model="form.sort" placeholder="请输入排序" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import {listBanner, getBanner, delBanner, addBanner, updateBanner, exportBanner} from "@/api/hospital/banner";
import {getToken} from "@/utils/auth";

export default {
	name: "Banner",
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// banner表格数据
			bannerList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				name: null,
				thumbnail: null,
				type: null,
				status: null,
			},
			// 表单参数
			form: {},
			upload: {
				// 是否禁用上传
				isUploading: false,
				// 设置上传的请求头部
				headers: {Authorization: "Bearer " + getToken()},
				// 上传的地址
				url: process.env.VUE_APP_BASE_API + "/uploadOssFile",
			},
			ossUrl: this.$ossUrl,
			rules: {
				thumbnail: [{ required: true, message: "请上传缩略图", trigger: "change" }],
				name: [{ required: true, message: "请填写banner名称", trigger: "blur" }]
			}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		/** 查询banner列表 */
		getList() {
			this.loading = true;
			listBanner(this.queryParams).then(response => {
				this.bannerList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				bannerId: null,
				name: null,
				link: null,
				appid: null,
				thumbnail: null,
				type: 0,
				status: 0,
				sort: 0,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.bannerId)
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		/** 新增按钮操作 */
		handleAdd() {
			this.reset();
			this.open = true;
			this.title = "添加banner";
		},
		/** 修改按钮操作 */
		handleUpdate(row) {
			this.reset();
			const bannerId = row.bannerId || this.ids
			getBanner(bannerId).then(response => {
				this.form = response.data;
				this.open = true;
				this.title = "修改banner";
			});
		},
		/** 提交按钮 */
		submitForm() {
			this.$refs["form"].validate(valid => {
				if (valid) {
					if (this.form.bannerId != null) {
						updateBanner(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							}
						});
					} else {
						addBanner(this.form).then(response => {
							if (response.code === 200) {
								this.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							}
						});
					}
				}
			});
		},
		/** 删除按钮操作 */
		handleDelete(row) {
			const bannerIds = row.bannerId || this.ids;
			this.$confirm('是否确认删除banner编号为"' + bannerIds + '"的数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return delBanner(bannerIds);
			}).then(() => {
				this.getList();
				this.msgSuccess("删除成功");
			}).catch(function () {
			});
		},
		/** 导出按钮操作 */
		handleExport() {
			const queryParams = this.queryParams;
			this.$confirm('是否确认导出所有banner数据项?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				return exportBanner(queryParams);
			}).then(response => {
				this.download(response.msg);
			}).catch(function () {
			});
		},
		handleImageSuccess(res) {
			this.form.thumbnail = res.fileName;
			if (this.uploadLoading) {
				this.uploadLoading.close();
			}
		},
		beforeImageUpload(file) {
			let videoTypes = ["image/bmp", "image/gif", "image/jpg", "image/jpeg", "image/png"];
			if (!videoTypes.includes(file.type)) {
				this.msgInfo('上传视频只能是 bmp/gif/jpg/jpeg/png 格式!');
				return false;
			}
			this.uploadLoading = this.$loading({
				lock: true,
				text: '加载中'
			});
			return true;
		}
	}
};
</script>
<style scoped>
.video_uploader ::v-deep.el-upload {
	border: 1px dashed #d9d9d9;
	border-radius: 6px;
	cursor: pointer;
	position: relative;
	overflow: hidden;
}
.video_uploader ::v-deep.el-upload:hover {
	border-color: #409EFF;
}
.video_uploader-icon {
	font-size: 28px;
	color: #8c939d;
	width: 178px;
	height: 178px;
	line-height: 178px;
	text-align: center;
}
.avatar {
	width: 178px;
	height: 178px;
	display: block;
}
</style>
